package org.lord.slark.numbers.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import org.lord.slark.numbers.R;
import org.lord.slark.numbers.db.LocalDB;
import org.lord.slark.numbers.entity.Saint;
import org.lord.slark.numbers.utils.ELog;
import org.lord.slark.numbers.utils.LocalConstants;

public class AddSaintActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText etname, etphone;
    private RadioButton rbmale, rbfemale;
    private Switch baptizeswitch;
    private TextView tvsave, tvcenter;
    private ImageView clearName, clearPhone;
    private int mSaintId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_add_saint);
        initView();
        dealWithIntent();
    }

    private void dealWithIntent() {
        Saint saint = (Saint) getIntent().getSerializableExtra(LocalConstants.SAINT);
        if (saint != null) {
            tvcenter.setText(getString(R.string.seditsaint));
            mSaintId = saint.getId();
            etname.setText(saint.getName());
            etphone.setText(saint.getPhone());
            baptizeswitch.setChecked(saint.getIsBaptized() == 1 ? true : false);
            if (saint.getGender() == 1) {
                rbmale.setChecked(true);
            } else {
                rbfemale.setChecked(true);
            }
        }
    }

    private void initView() {
        etname = (EditText) findViewById(R.id.etname);
        etphone = (EditText) findViewById(R.id.etphone);
        rbmale = (RadioButton) findViewById(R.id.rbmale);
        rbfemale = (RadioButton) findViewById(R.id.rbfemale);
        baptizeswitch = (Switch) findViewById(R.id.bapswitch);
        tvsave = (TextView) findViewById(R.id.tvsave);
        tvcenter = (TextView) findViewById(R.id.tvcenter);
        clearName = (ImageView) findViewById(R.id.clearName);
        clearPhone = (ImageView) findViewById(R.id.clearPhone);
        tvsave.setOnClickListener(this);
        clearName.setOnClickListener(this);
        clearPhone.setOnClickListener(this);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvsave:
                String sql = null;
                if (mSaintId != -1) {
                    sql = "update " + Saint.TABLE_NAME + " set "
                            + Saint.COLUMN_GENDER + " = ?,"
                            + Saint.COLUMN_NAME + " = ?,"
                            + Saint.COLUMN_PHONE + " = ?,"
                            + Saint.COLUMN_IS_BAPTIZED + " = ? where " + Saint.COLUMN_ID + " = " + mSaintId + ";";
                } else {
                    sql = "insert into " + Saint.TABLE_NAME + " values (NULL, ?, ?, ?, ?);";
                }
                String name = etname.getText().toString().trim();
                String phone = etphone.getText().toString().trim();
                int gender = rbmale.isChecked() ? 1 : 0;
                int isbaptized = baptizeswitch.isChecked() ? 1 : 0;
                if (name == null || name.length() == 0) {
                    Toast.makeText(this, getResources().getString(R.string.nametoast), Toast.LENGTH_LONG).show();
                    return;
                }
                if (phone == null || phone.length() == 0) {
                    Toast.makeText(this, getResources().getString(R.string.phonetoast), Toast.LENGTH_LONG).show();
                    return;
                }
                try {
                    LocalDB.getInstance(this).getWritableDatabase().execSQL(sql, new Object[]{gender, name, phone, isbaptized});
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Saint s = new Saint();
                s.setName(name);
                s.setPhone(phone);
                setResult(RESULT_OK, new Intent().putExtra(LocalConstants.SAINT, s));
                finish();
                break;
            case R.id.clearName:
                etname.setText("");
                break;
            case R.id.clearPhone:
                etphone.setText("");
                break;
            default:
                break;
        }
    }
}
